﻿#region using

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web; 

#endregion

namespace Sharp.Framework.Extensions
{
    /// <summary>
    ///  字符串辅助类
    /// </summary>
    public static class StringExtensions
    {

        /// <summary>
        /// 获取ContentType
        /// </summary>
        /// <param name="theString"></param>
        /// <returns></returns>
        public static string GetContentType(this string theString)
        {
            Dictionary<string, string> maps = new Dictionary<string, string>
                                                  {
                                                      {".001", "application/x-001"},
                                                      {".301", "application/x-301"},
                                                      {".323", "text/h323"},
                                                      {".906", "application/x-906"},
                                                      {".907", "drawing/907"},
                                                      {".a11", "application/x-a11"},
                                                      {".acp", "audio/x-mei-aac"},
                                                      {".ai", "application/postscript"},
                                                      {".aif", "audio/aiff"},
                                                      {".aifc", "audio/aiff"},
                                                      {".aiff", "audio/aiff"},
                                                      {".anv", "application/x-anv"},
                                                      {".asa", "text/asa"},
                                                      {".asf", "video/x-ms-asf"},
                                                      {".asp", "text/asp"},
                                                      {".asx", "video/x-ms-asf"},
                                                      {".au", "audio/basic"},
                                                      {".avi", "video/avi"},
                                                      {".awf", "application/vnd.adobe.workflow"},
                                                      {".biz", "text/xml"},
                                                      {".bmp", "application/x-bmp"},
                                                      {".bot", "application/x-bot"},
                                                      {".c4t", "application/x-c4t"},
                                                      {".c90", "application/x-c90"},
                                                      {".cal", "application/x-cals"},
                                                      {".cat", "application/vnd.ms-pki.seccat"},
                                                      {".cdf", "application/x-netcdf"},
                                                      {".cdr", "application/x-cdr"},
                                                      {".cel", "application/x-cel"},
                                                      {".cer", "application/x-x509-ca-cert"},
                                                      {".cg4", "application/x-g4"},
                                                      {".cgm", "application/x-cgm"},
                                                      {".cit", "application/x-cit"},
                                                      {".class", "java/*"},
                                                      {".cml", "text/xml"},
                                                      {".cmp", "application/x-cmp"},
                                                      {".cmx", "application/x-cmx"},
                                                      {".cot", "application/x-cot"},
                                                      {".crl", "application/pkix-crl"},
                                                      {".crt", "application/x-x509-ca-cert"},
                                                      {".csi", "application/x-csi"},
                                                      {".css", "text/css"},
                                                      {".cut", "application/x-cut"},
                                                      {".dbf", "application/x-dbf"},
                                                      {".dbm", "application/x-dbm"},
                                                      {".dbx", "application/x-dbx"},
                                                      {".dcd", "text/xml"},
                                                      {".dcx", "application/x-dcx"},
                                                      {".der", "application/x-x509-ca-cert"},
                                                      {".dgn", "application/x-dgn"},
                                                      {".dib", "application/x-dib"},
                                                      {".dll", "application/x-msdownload"},
                                                      {".doc", "application/msword"},
                                                      {".dot", "application/msword"},
                                                      {".drw", "application/x-drw"},
                                                      {".dtd", "text/xml"},
                                                      {".dwf", "application/x-dwf"},
                                                      {".dwg", "application/x-dwg"},
                                                      {".dxb", "application/x-dxb"},
                                                      {".dxf", "application/x-dxf"},
                                                      {".edn", "application/vnd.adobe.edn"},
                                                      {".emf", "application/x-emf"},
                                                      {".eml", "message/rfc822"},
                                                      {".ent", "text/xml"},
                                                      {".epi", "application/x-epi"},
                                                      {".eps", "application/x-ps"},
                                                      {".etd", "application/x-ebx"},
                                                      {".exe", "application/x-msdownload"},
                                                      {".fax", "image/fax"},
                                                      {".fdf", "application/vnd.fdf"},
                                                      {".fif", "application/fractals"},
                                                      {".fo", "text/xml"},
                                                      {".frm", "application/x-frm"},
                                                      {".g4", "application/x-g4"},
                                                      {".gbr", "application/x-gbr"},
                                                      {".gcd", "application/x-gcd"},
                                                      {".gif", "image/gif"},
                                                      {".gl2", "application/x-gl2"},
                                                      {".gp4", "application/x-gp4"},
                                                      {".hgl", "application/x-hgl"},
                                                      {".hmr", "application/x-hmr"},
                                                      {".hpg", "application/x-hpgl"},
                                                      {".hpl", "application/x-hpl"},
                                                      {".hqx", "application/mac-binhex40"},
                                                      {".hrf", "application/x-hrf"},
                                                      {".hta", "application/hta"},
                                                      {".htc", "text/x-component"},
                                                      {".htm", "text/html"},
                                                      {".html", "text/html"},
                                                      {".htt", "text/webviewhtml"},
                                                      {".htx", "text/html"},
                                                      {".icb", "application/x-icb"},
                                                      {".ico", "image/x-icon"},
                                                      {".iff", "application/x-iff"},
                                                      {".ig4", "application/x-g4"},
                                                      {".igs", "application/x-igs"},
                                                      {".iii", "application/x-iphone"},
                                                      {".img", "application/x-img"},
                                                      {".ins", "application/x-internet-signup"},
                                                      {".isp", "application/x-internet-signup"},
                                                      {".IVF", "video/x-ivf"},
                                                      {".java", "java/*"},
                                                      {".jfif", "image/jpeg"},
                                                      {".jpe", "image/jpeg"},
                                                      {".jpeg", "image/jpeg"},
                                                      {".js", "application/x-javascript"},
                                                      {".jsp", "text/html"},
                                                      {".la1", "audio/x-liquid-file"},
                                                      {".lar", "application/x-laplayer-reg"},
                                                      {".latex", "application/x-latex"},
                                                      {".lavs", "audio/x-liquid-secure"},
                                                      {".lbm", "application/x-lbm"},
                                                      {".lmsff", "audio/x-la-lms"},
                                                      {".ls", "application/x-javascript"},
                                                      {".ltr", "application/x-ltr"},
                                                      {".m1v", "video/x-mpeg"},
                                                      {".m2v", "video/x-mpeg"},
                                                      {".m3u", "audio/mpegurl"},
                                                      {".m4e", "video/mpeg4"},
                                                      {".mac", "application/x-mac"},
                                                      {".man", "application/x-troff-man"},
                                                      {".math", "text/xml"},
                                                      {".mdb", "application/msaccess"},
                                                      {".mfp", "application/x-shockwave-flash"},
                                                      {".mht", "message/rfc822"},
                                                      {".mhtml", "message/rfc822"},
                                                      {".mi", "application/x-mi"},
                                                      {".mid", "audio/mid"},
                                                      {".midi", "audio/mid"},
                                                      {".mil", "application/x-mil"},
                                                      {".mml", "text/xml"},
                                                      {".mnd", "audio/x-musicnet-download"},
                                                      {".mns", "audio/x-musicnet-stream"},
                                                      {".mocha", "application/x-javascript"},
                                                      {".movie", "video/x-sgi-movie"},
                                                      {".mp1", "audio/mp1"},
                                                      {".mp2", "audio/mp2"},
                                                      {".mp2v", "video/mpeg"},
                                                      {".mp3", "audio/mp3"},
                                                      {".mp4", "video/mpeg4"},
                                                      {".mpa", "video/x-mpg"},
                                                      {".mpd", "application/vnd.ms-project"},
                                                      {".mpe", "video/x-mpeg"},
                                                      {".mpeg", "video/mpg"},
                                                      {".mpp", "application/vnd.ms-project"},
                                                      {".mps", "video/x-mpeg"},
                                                      {".mpt", "application/vnd.ms-project"},
                                                      {".mpv", "video/mpg"},
                                                      {".mpv2", "video/mpeg"},
                                                      {".mpw", "application/vnd.ms-project"},
                                                      {".mpx", "application/vnd.ms-project"},
                                                      {".mtx", "text/xml"},
                                                      {".mxp", "application/x-mmxp"},
                                                      {".net", "image/pnetvue"},
                                                      {".nrf", "application/x-nrf"},
                                                      {".nws", "message/rfc822"},
                                                      {".odc", "text/x-ms-odc"},
                                                      {".out", "application/x-out"},
                                                      {".p10", "application/pkcs10"},
                                                      {".p12", "application/x-pkcs12"},
                                                      {".p7b", "application/x-pkcs7-certificates"},
                                                      {".p7c", "application/pkcs7-mime"},
                                                      {".p7m", "application/pkcs7-mime"},
                                                      {".p7r", "application/x-pkcs7-certreqresp"},
                                                      {".p7s", "application/pkcs7-signature"},
                                                      {".pc5", "application/x-pc5"},
                                                      {".pci", "application/x-pci"},
                                                      {".pcl", "application/x-pcl"},
                                                      {".pcx", "application/x-pcx"},
                                                      {".pdf", "application/pdf"},
                                                      {".pdx", "application/vnd.adobe.pdx"},
                                                      {".pfx", "application/x-pkcs12"},
                                                      {".pgl", "application/x-pgl"},
                                                      {".pic", "application/x-pic"},
                                                      {".pko", "application/vnd.ms-pki.pko"},
                                                      {".pl", "application/x-perl"},
                                                      {".plg", "text/html"},
                                                      {".pls", "audio/scpls"},
                                                      {".plt", "application/x-plt"},
                                                      {".png", "image/png"},
                                                      {".pot", "application/vnd.ms-powerpoint"},
                                                      {".ppa", "application/vnd.ms-powerpoint"},
                                                      {".ppm", "application/x-ppm"},
                                                      {".pps", "application/vnd.ms-powerpoint"},
                                                      {".ppt", "application/vnd.ms-powerpoint"},
                                                      {".pr", "application/x-pr"},
                                                      {".prf", "application/pics-rules"},
                                                      {".prn", "application/x-prn"},
                                                      {".ps", "application/x-ps"},
                                                      {".ptn", "application/x-ptn"},
                                                      {".pwz", "application/vnd.ms-powerpoint"},
                                                      {".r3t", "text/vnd.rn-realtext3d"},
                                                      {".ra", "audio/vnd.rn-realaudio"},
                                                      {".ram", "audio/x-pn-realaudio"},
                                                      {".ras", "application/x-ras"},
                                                      {".rat", "application/rat-file"},
                                                      {".rdf", "text/xml"},
                                                      {".rec", "application/vnd.rn-recording"},
                                                      {".red", "application/x-red"},
                                                      {".rgb", "application/x-rgb"},
                                                      {".rjs", "application/vnd.rn-realsystem-rjs"},
                                                      {".rjt", "application/vnd.rn-realsystem-rjt"},
                                                      {".rlc", "application/x-rlc"},
                                                      {".rle", "application/x-rle"},
                                                      {".rm", "application/vnd.rn-realmedia"},
                                                      {".rmf", "application/vnd.adobe.rmf"},
                                                      {".rmi", "audio/mid"},
                                                      {".rmm", "audio/x-pn-realaudio"},
                                                      {".rmp", "application/vnd.rn-rn_music_package"},
                                                      {".rms", "application/vnd.rn-realmedia-secure"},
                                                      {".rmvb", "application/vnd.rn-realmedia-vbr"},
                                                      {".rmx", "application/vnd.rn-realsystem-rmx"},
                                                      {".rnx", "application/vnd.rn-realplayer"},
                                                      {".rp", "image/vnd.rn-realpix"},
                                                      {".rpm", "audio/x-pn-realaudio-plugin"},
                                                      {".rsml", "application/vnd.rn-rsml"},
                                                      {".rt", "text/vnd.rn-realtext"},
                                                      {".rtf", "application/msword"},
                                                      {".rv", "video/vnd.rn-realvideo"},
                                                      {".sam", "application/x-sam"},
                                                      {".sat", "application/x-sat"},
                                                      {".sdp", "application/sdp"},
                                                      {".sdw", "application/x-sdw"},
                                                      {".sit", "application/x-stuffit"},
                                                      {".slb", "application/x-slb"},
                                                      {".sld", "application/x-sld"},
                                                      {".slk", "drawing/x-slk"},
                                                      {".smi", "application/smil"},
                                                      {".smil", "application/smil"},
                                                      {".smk", "application/x-smk"},
                                                      {".snd", "audio/basic"},
                                                      {".sol", "text/plain"},
                                                      {".sor", "text/plain"},
                                                      {".spc", "application/x-pkcs7-certificates"},
                                                      {".spl", "application/futuresplash"},
                                                      {".spp", "text/xml"},
                                                      {".ssm", "application/streamingmedia"},
                                                      {".sst", "application/vnd.ms-pki.certstore"},
                                                      {".stl", "application/vnd.ms-pki.stl"},
                                                      {".stm", "text/html"},
                                                      {".sty", "application/x-sty"},
                                                      {".svg", "text/xml"},
                                                      {".swf", "application/x-shockwave-flash"},
                                                      {".tdf", "application/x-tdf"},
                                                      {".tg4", "application/x-tg4"},
                                                      {".tga", "application/x-tga"},
                                                      {".tif", "image/tiff"},
                                                      {".tiff", "image/tiff"},
                                                      {".tld", "text/xml"},
                                                      {".top", "drawing/x-top"},
                                                      {".torrent", "application/x-bittorrent"},
                                                      {".tsd", "text/xml"},
                                                      {".txt", "text/plain"},
                                                      {".uin", "application/x-icq"},
                                                      {".uls", "text/iuls"},
                                                      {".vcf", "text/x-vcard"},
                                                      {".vda", "application/x-vda"},
                                                      {".vdx", "application/vnd.visio"},
                                                      {".vml", "text/xml"},
                                                      {".vpg", "application/x-vpeg005"},
                                                      {".vsd", "application/vnd.visio"},
                                                      {".vss", "application/vnd.visio"},
                                                      {".vst", "application/vnd.visio"},
                                                      {".vsw", "application/vnd.visio"},
                                                      {".vsx", "application/vnd.visio"},
                                                      {".vtx", "application/vnd.visio"},
                                                      {".vxml", "text/xml"},
                                                      {".wav", "audio/wav"},
                                                      {".wax", "audio/x-ms-wax"},
                                                      {".wb1", "application/x-wb1"},
                                                      {".wb2", "application/x-wb2"},
                                                      {".wb3", "application/x-wb3"},
                                                      {".wbmp", "image/vnd.wap.wbmp"},
                                                      {".wiz", "application/msword"},
                                                      {".wk3", "application/x-wk3"},
                                                      {".wk4", "application/x-wk4"},
                                                      {".wkq", "application/x-wkq"},
                                                      {".wks", "application/x-wks"},
                                                      {".wm", "video/x-ms-wm"},
                                                      {".wma", "audio/x-ms-wma"},
                                                      {".wmd", "application/x-ms-wmd"},
                                                      {".wmf", "application/x-wmf"},
                                                      {".wml", "text/vnd.wap.wml"},
                                                      {".wmv", "video/x-ms-wmv"},
                                                      {".wmx", "video/x-ms-wmx"},
                                                      {".wmz", "application/x-ms-wmz"},
                                                      {".wp6", "application/x-wp6"},
                                                      {".wpd", "application/x-wpd"},
                                                      {".wpg", "application/x-wpg"},
                                                      {".wpl", "application/vnd.ms-wpl"},
                                                      {".wq1", "application/x-wq1"},
                                                      {".wr1", "application/x-wr1"},
                                                      {".wri", "application/x-wri"},
                                                      {".wrk", "application/x-wrk"},
                                                      {".ws", "application/x-ws"},
                                                      {".ws2", "application/x-ws"},
                                                      {".wsc", "text/scriptlet"},
                                                      {".wsdl", "text/xml"},
                                                      {".wvx", "video/x-ms-wvx"},
                                                      {".xdp", "application/vnd.adobe.xdp"},
                                                      {".xdr", "text/xml"},
                                                      {".xfd", "application/vnd.adobe.xfd"},
                                                      {".xfdf", "application/vnd.adobe.xfdf"},
                                                      {".xhtml", "text/html"},
                                                      {".xls", "application/vnd.ms-excel"},
                                                      {".xlw", "application/x-xlw"},
                                                      {".xml", "text/xml"},
                                                      {".xpl", "audio/scpls"},
                                                      {".xq", "text/xml"},
                                                      {".xql", "text/xml"},
                                                      {".xquery", "text/xml"},
                                                      {".xsd", "text/xml"},
                                                      {".xsl", "text/xml"},
                                                      {".xslt", "text/xml"},
                                                      {".xwd", "application/x-xwd"},
                                                      {".x_b", "application/x-x_b"},
                                                      {".x_t", "application/x-x_t"}
                                                  };
            return maps.ContainsKey(theString) ? maps[theString] : "application/octet-stream";
        }

        /// <summary>
        /// 邮件对象名中文字符编码
        /// </summary>
        /// <param name="theString"></param>
        /// <returns></returns>
        public static string EmailMimeNameDecoding(this string theString)
        {
            if (string.IsNullOrEmpty(theString))
            {
                return string.Empty;
            }
            if (Regex.IsMatch(theString, @"^[a-zA-Z._0-9]+$"))
            {
                return theString;
            }
            else
            {

                byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(theString);
                string keyword = Convert.ToBase64String(buffer);
                return string.Format("=?gb2312?B?{0}?=", keyword);
            }

        }

        /// <summary>
        /// 邮件名称解码
        /// </summary>
        /// <param name="theString">字符串</param>
        /// <returns>返回判断结果</returns>
        public static string EmailNameDecoding(this string theString)
        {
            if (string.IsNullOrEmpty(theString))
            {
                return string.Empty;
            }
            var fileNames = theString.Split('?');
            if (fileNames.Length != 5 || fileNames[2] != "B")
            {
                return theString;
            }
            try
            {
                string encodingstr = "";
                string splitstr = fileNames[1].ToUpper();
                encodingstr = fileNames[1] == "UTF8" ? "utf-8" : fileNames[1];
                var encode = Encoding.GetEncoding(encodingstr); 
                return null != encode ? encode.GetString(Convert.FromBase64String(fileNames[3])) : theString;
            }
            catch (FormatException)
            {
                return theString;
            }
            catch (DecoderFallbackException)
            {
                return theString;
            }
        }

        /// <summary>
        /// 判断字符串是否为null或者空
        /// </summary>
        /// <param name="theString">字符串</param>
        /// <returns>返回判断结果</returns>
        public static bool IsNullOrEmpty(this string theString)
        {
            return string.IsNullOrEmpty(theString);
        }

        /// <summary>
        /// 把null的字符串对象转为空字符串
        /// </summary>
        /// <param name="queryString">字符串</param>
        /// <returns>返回空字符串</returns>
        public static string ToDefaultOrEmpty(this string queryString)
        {
            return string.IsNullOrEmpty(queryString) ? string.Empty : queryString;
        }

        /// <summary>
        /// 判断是字符串是否非null或者非空
        /// </summary>
        /// <param name="theString">字符串对象</param>
        /// <returns>返回判断结果</returns>
        public static bool IsNotNullOrEmpty(this string theString)
        {
            return !string.IsNullOrEmpty(theString);
        }

        /// <summary>
        /// 指定格式的字符串
        /// </summary>
        /// <param name="theString">字符串</param>
        /// <param name="args">参数</param>
        /// <returns>返回指定格式的字符串</returns>
        public static string FormatWith(this string theString, params object[] args)
        {
            return string.Format(theString, args);
        }

        /// <summary>
        /// 字符串截取(从前往后截取)
        /// </summary>
        /// <param name="theString">字符串</param>
        /// <param name="index">长度</param>
        /// <returns>截断后的字符串</returns>
        public static string FirstNCharacters(this string theString, int index)
        {
            if (theString == null)
                return string.Empty;

            int length = theString.Trim().Length;

            if (index >= length)
            {
                index = length;
            }

            return theString.Substring(0, index);
        }

        /// <summary>
        /// 字符串截取(从后往前截取)
        /// </summary>
        /// <param name="theString">字符串</param>
        /// <param name="index">长度</param>
        /// <returns>截取后的字符串</returns>
        public static string LastNCharacters(this string theString, int index)
        {
            if (theString == null)
                return string.Empty;

            int length = theString.Trim().Length;

            if (index > length)
            {
                throw new InvalidOperationException("string must be longer than the index value");
            }

            return theString.Substring((length - index), index);
        }

        /// <summary>
        /// 在字符串前加回车
        /// </summary>
        /// <param name="sb">处理后的字符串</param>
        /// <param name="theString">要处理的字符串</param>
        public static void AppendHtmlLineIfNotEmpty(this StringBuilder sb, string theString)
        {
            if (!theString.IsNullOrEmpty())
            {
                sb.AppendFormat("<BR />{0}", theString);
            }
        }

        /// <summary>
        /// 转换成Html内容
        /// </summary> 
        /// <param name="theString">要处理的字符串</param>
        public static string ConvertToHtml(this string theString)
        {
            if (!theString.IsNullOrEmpty())
            {
                if (!(theString.ToLower().Contains("<html") && theString.ToLower().Contains("</html") &&
                      theString.ToLower().Contains("<body") && theString.ToLower().Contains("</body")))
                    theString = string.Format(
                        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html><head><title></title> <base target=\"_blank\" /></head><body>{0}</body></html>",
                        theString);
            }
            else
            {
                theString = string.Empty;
            }
            return theString;

        }

        /// <summary>
        /// 转换Html 链接标签的target 为指定 target
        /// </summary> 
        /// <param name="theString">要处理的字符串</param>
        /// <param name="target"></param>
        public static string ConvertHtmlTarget(this string theString, string target)
        {
            if (theString.IsNullOrEmpty())
            {
                return string.Empty;
            }
            theString = Regex.Replace(theString, "(?<=target=(\"|'))\\w*", target,
                                      RegexOptions.IgnoreCase);
            return theString;

        }

        /// <summary>
        ///  获取字符串前几个奇数的词<br/>
        /// (例如 "one two three four five six"取前3个为"one three five" 注意中间用空格隔开)
        /// </summary>
        /// <param name = "theString">原字符串</param>
        /// <param name = "numWords">词数</param>
        /// <returns>
        /// 返回处理后的字符串
        /// </returns>
        public static string FirstNWords(this string theString, int numWords)
        {
            StringBuilder sb = new StringBuilder();

            if (theString != null && numWords >= 0)
            {
                string[] words = theString.Split(' ');

                IEnumerator enumerator = words.GetEnumerator();

                int count = 0;
                while (count < numWords)
                {
                    if (count != 0)
                    {
                        sb.Append(" ");
                    }

                    if (enumerator.MoveNext())
                    {
                        sb.Append(enumerator.Current);
                        enumerator.MoveNext();
                        count++;
                    }
                    else
                    {
                        // die quietly
                        break;
                    }
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// 字符串转换为DateTime(如果不能转化返回null)
        /// </summary>
        /// <param name="theString">字符串</param>
        /// <returns>时间格式</returns>
        public static DateTime? ToDateTime(this string theString)
        {
            if (!string.IsNullOrEmpty(theString))
            {
                DateTime result;
                if (DateTime.TryParse(theString, out result))
                {
                    return result;
                }
            }
            return null;
        }

        /// <summary>
        /// 字符串转换为int(如果不能转换则返回null)
        /// </summary>
        /// <param name="theString"></param>
        /// <returns></returns>
        public static int? ToInt(this string theString)
        {
            if (!string.IsNullOrEmpty(theString))
            {
                int result;
                if (int.TryParse(theString, out result))
                {
                    return result;
                }
            }
            return null;
        }

        /// <summary>
        /// 添加Url参数到Url字符串后面
        /// </summary>
        /// <param name="theString">要处理的字符串</param>
        /// <param name="paras">参数的集合</param>
        /// <returns>返回带有参数的url</returns>
        public static string AddUrlParas(this string theString, IDictionary<string, string> paras)
        {
            if (null == theString)
            {
                theString = string.Empty;
            }
            var norparastr = string.Join("&",
                                         paras.Select(
                                             m => string.Format("{0}={1}", m.Key, HttpUtility.UrlEncode(m.Value))).
                                             ToArray());
            if (theString.Contains("?"))
            {
                return string.Format("{0}&{1}", theString, norparastr);
            }
            return string.Format("{0}?{1}", theString, norparastr);
        }

        /// <summary>
        /// MD5加密码字符串
        /// </summary>
        /// <param name="theString">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string ToMD5(this string theString)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            string encoded =
                BitConverter.ToString(md5.ComputeHash(new UTF8Encoding(false).GetBytes(theString))).Replace("-", "");
            return encoded;
        }
         

        /// <summary>
        /// 将查询字符串解析转换为名值集合
        /// </summary>
        /// <param name="queryString">字符串</param>
        /// <returns>名值集合</returns>
        public static NameValueCollection GetQueryString(this string queryString)
        {
            return queryString.GetQueryString(Encoding.UTF8);
        }

        /// <summary>
        /// 将查询字符串解析转换为名值集合(指定编码格式)
        /// </summary>
        /// <param name="queryString">字符串</param>
        /// <param name="encoding">编码格式</param>
        /// <returns>名值集合</returns>
        public static NameValueCollection GetQueryString(this string queryString, Encoding encoding)
        {

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            queryString = queryString.Replace("?", "");
            var result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
            if (!string.IsNullOrEmpty(queryString))
            {
                int count = queryString.Length;
                for (int i = 0; i < count; i++)
                {
                    int startIndex = i;
                    int index = -1;
                    while (i < count)
                    {
                        char item = queryString[i];
                        if (item == '=')
                        {
                            if (index < 0)
                            {
                                index = i;
                            }
                        }
                        else if (item == '&')
                        {
                            break;
                        }
                        i++;
                    }
                    string key = null;
                    string value = null;
                    if (index >= 0)
                    {
                        key = queryString.Substring(startIndex, index - startIndex);
                        value = queryString.Substring(index + 1, (i - index) - 1);
                    }
                    else
                    {
                        key = queryString.Substring(startIndex, i - startIndex);
                    }
                    result[HttpUtility.UrlDecode(key, encoding)] = HttpUtility.UrlDecode(value, encoding);


                    if ((i == (count - 1)) && (queryString[i] == '&'))
                    {
                        result[key] = string.Empty;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 字符串时间格式(可指定时间格式,不能转换则返回空字符串)
        /// </summary>
        /// <param name="queryString">字符串</param>
        /// <param name="format">时间格式</param>
        /// <returns>时间</returns>
        public static string ToDateTimeString(this string queryString, string format)
        {
            DateTime dt;
            return DateTime.TryParse(queryString, out dt) ? dt.ToString(format) : string.Empty;
        }

        /// <summary>
        /// 字符截断后加...(中英文特殊处理)<br/>
        /// </summary>
        /// <param name="queryString">要截取的字符串</param>
        /// <param name="length">长度</param>
        /// <returns>截断后的字符串</returns>
        public static string CutString(this string queryString, int length)
        {
            if (string.IsNullOrEmpty(queryString))
            {
                return string.Empty;
            }
            if (queryString.Length <= length)
            {
                if (Regex.IsMatch(queryString, @"[\u4e00-\u9fa5]+"))
                {
                    var temp = Regex.Replace(queryString, "[\u4e00-\u9fa5]", "ab");
                    if (temp.Length <= length)
                    {
                        return queryString;
                    }
                }
                else
                {
                    return queryString;
                }
            }

            var index = 0;
            for (int i = 0; i < queryString.Length; i++)
            {

                //js写法 if ( ! new RegExp("[u4e00-u9fa5]").test(temp.substr(i, 1)) )  
                if (Regex.IsMatch(queryString.Substring(i, 1), @"[\u4e00-\u9fa5]+"))
                {
                    index += 2;
                }
                else
                {
                    index += 1;
                }
                if (index == length)
                {
                    queryString = queryString.Substring(0, i + 1);
                    break;
                }
                if (index == length + 1)
                {
                    queryString = queryString.Substring(0, i);
                    break;
                }
            }
            return queryString + "...";
        }

        /// <summary>
        /// 高亮显示
        /// </summary>
        /// <param name="queryString">字符串</param>
        /// <param name="keyword">关键字 </param>
        /// <returns></returns>
        public static string HightLightString(this string queryString, string keyword )
        {
            return HightLightString(queryString, keyword, "<font style=\"color:red;\">{0}</font>");
        }


        /// <summary>
        /// 高亮显示
        /// </summary>
        /// <param name="queryString">字符串</param>
        /// <param name="keyword">关键字 </param>
        /// <param name="htmlLable">要替换成的标签(格式如:&lt;font style="color:red;"&gt;{0}&lt;/font&gt;)</param>
        /// <returns></returns>
        public static string HightLightString(this string queryString,string keyword, string htmlLable)
        {
            if (string.IsNullOrEmpty(keyword))
                return queryString;
            if(Regex.IsMatch(queryString,keyword,RegexOptions.IgnoreCase))
            {
                var macths = Regex.Matches(queryString, keyword, RegexOptions.IgnoreCase);
                if (htmlLable.IndexOf(keyword, System.StringComparison.Ordinal) != -1)
                {
                    queryString = macths.Cast<object>().Aggregate(queryString,
                                                              (current, macth) =>
                                                              current.Replace(macth.ToString(),
                                                                              String.Format("[{0}]", macth)));
                    while (queryString.IndexOf("[[", System.StringComparison.Ordinal) != -1)
                    {
                        queryString = queryString.Replace("[[", "[");
                    }
                    while (queryString.IndexOf("]]", System.StringComparison.Ordinal) != -1)
                    {
                        queryString = queryString.Replace("]]", "]");
                    }
                    queryString = macths.Cast<object>().Aggregate(queryString,
                                                              (current, macth) =>
                                                              current.Replace(String.Format("[{0}]", macth),
                                                                              String.Format(htmlLable, macth)));
                    return queryString;
                }
                queryString = macths.Cast<object>().Aggregate(queryString, (current, macth) => current.Replace(macth.ToString(), String.Format(htmlLable, macth)));
            }
            return queryString;
        }

        /// <summary>
        /// 获取替换结果
        /// </summary>
        /// <param name="theString">字符串</param>
        /// <param name="replace"></param>
        /// <returns></returns>
        public static string GetReplace(this string theString, Dictionary<string, string> replace)
        {
            if (replace == null) return theString;

            theString = replace.Aggregate(theString, (current, r) => current.Replace(r.Key, r.Value));

            return theString;
        }
    }
}
